﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Freejobber.Core.Data;
using Freejobber.Core.Generic.Helpers;
using System.Transactions;

namespace Freejobber.Core.Generic.Profile
{
    public class UserClass
    {
        #region Fields
        private long id;
        private string name;
        private string email;
        private string description;
        private int profileEnabled = 0;
        private DateTime? lastactivity = null;
        private List<string> cities = new List<string>();
        private string phone1;
        private string phone2;
        private string photo;
        private string userName;
        private tbl_Users tbl_user;
        private byte workForm;
        private static db_freejobberEntities dbContext= db_freejobberEntities.GetConnecton();   
        #endregion

        #region Properties
        public long Id
        {
            get { return id; }
            set { id = value; }
        }

        public string LoginName
        {
            get { return tbl_user.LoginName; }
            set 
            {
                name = value;
                tbl_user.LoginName = name;
               // dbContext.SaveChanges();
            }
        }

        public string Email
        {
            get
            {
                if (string.IsNullOrEmpty(email))
                {
                    email = tbl_user.Email;
                }
                return email;
            }
            set 
            { 
                email = value;
                tbl_user.Email = email;
               
            }
        }

        public string Photo
        {
            get { return tbl_user.tbl_UserProfile.Photo; }
            set 
                {
                photo = value;
                tbl_user.tbl_UserProfile.Photo = photo;
                }
        }

        public List<string> Cities
        {
            get { return new List<string>() { "1", "2" }; }
        }

        public DateTime? Lastactivity
        {
            get 
            {
                if (tbl_user.tbl_UserProfile.LastActivity == null)
                    return DateTime.Now;
                return tbl_user.tbl_UserProfile.LastActivity; 
            }
            set
            {
                lastactivity = value;
                tbl_user.tbl_UserProfile.LastActivity = lastactivity;
            }
        }

        public int ProfileEnabled
        {
            get
            {
                if (string.IsNullOrEmpty(name))
                    profileEnabled = 1;
                else if (string.IsNullOrEmpty(phone1) || string.IsNullOrEmpty(phone2))
                    profileEnabled = 2;
                else
                    profileEnabled = 3;

                return profileEnabled;
            }
        }

        public string Description
        {
            get { return tbl_user.tbl_UserProfile.Description; }
            set 
            {
                description = value;
                tbl_user.tbl_UserProfile.Description = description;
            }
        }

        public string Phone1
        {
            get { return tbl_user.tbl_UserProfile.CellPhone1; }
            set 
            { 
                phone1 = value;
                tbl_user.tbl_UserProfile.CellPhone1 = phone1;
            }
        }

        public string Phone2
        {
            get { return tbl_user.tbl_UserProfile.CellPhone2; }
            set
            {
                phone2 = value;
                tbl_user.tbl_UserProfile.CellPhone2 = phone2;
            }
        }

        public string UserName
        {
            get { return tbl_user.tbl_UserProfile.UserName; }
            set
            {
                userName = value;
                tbl_user.tbl_UserProfile.UserName = userName;
            }
        }
        public byte WorkForm
        {
            get {
                return tbl_user.tbl_UserProfile.WorkForm;
               // return workForm; 
            }
            set { workForm = value;
            tbl_user.tbl_UserProfile.WorkForm = workForm;
            
            }
        }

        #endregion

        #region Constructors
        public UserClass(long id)
        {         
            this.tbl_user = dbContext.tbl_Users.SingleOrDefault(x => x.Id == id); //TODO: Исключение
            if (tbl_user == null)
                return;
            // LoginName = tbl_user.LoginName;
            // Id = id;
            // Email = tbl_user.tbl_UserProfile.email;
        }

        public UserClass(string login)
        {
            this.tbl_user = dbContext.tbl_Users.SingleOrDefault(x => x.LoginName == login); //TODO: Исключение
            if (tbl_user == null)
                return;
            // LoginName = tbl_user.LoginName;
            // Id = id;
            // Email = tbl_user.tbl_UserProfile.email;
        }

        private UserClass()
        {

        }


        #endregion

        #region Private methods



        #endregion

        #region Public methods


        public void Save()
        {
            try
            {
                dbContext.SaveChanges();
            }
            catch { }
        }
      
        /// <summary>
        /// Проверка, занят ли логин.
        /// </summary>
        /// <param name="loginName"></param>
        /// <returns>true - занят</returns>
        public static bool IsLoginExist(string loginName)
        {
            bool result = false;
            try
            {
                dbContext = db_freejobberEntities.GetConnecton();
                result=dbContext.tbl_Users.Any(x => x.LoginName == loginName);
            }
            catch
            {
                result = true;
            }
            return result;
        }
        #endregion

    }
}